package com.ruoyi.project.ledger.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.ledger.domain.LedgerBusinessAccount;
import com.ruoyi.project.ledger.domain.LedgerFile;
import com.ruoyi.project.ledger.mapper.LedgerBusinessAccountMapper;
import com.ruoyi.project.ledger.mapper.LedgerFileMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerForkliftMapper;
import com.ruoyi.project.ledger.domain.LedgerForklift;
import com.ruoyi.project.ledger.service.ILedgerForkliftService;

/**
 * 叉车台账Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-05-08
 */
@Service
public class LedgerForkliftServiceImpl implements ILedgerForkliftService 
{

    private static final Logger log = LoggerFactory.getLogger(LedgerForkliftServiceImpl.class);

    @Autowired
    private LedgerForkliftMapper ledgerForkliftMapper;
    @Autowired
    private LedgerBusinessAccountMapper ledgerBusinessAccountMapper;
    @Autowired
    private LedgerFileMapper ledgerFileMapper;


    /**
     * 查询叉车台账
     * 
     * @param id 叉车台账ID
     * @return 叉车台账
     */
    @Override
    public LedgerForklift selectLedgerForkliftById(Long id)
    {
        LedgerForklift forklift = ledgerForkliftMapper.selectLedgerForkliftById(id);
        if(forklift != null){
            // 获取图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("6");      // 叉车台账
            List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList(ledgerFile);
            forklift.setImgList(imgList);
        }
        return forklift;
    }

    /**
     * 查询叉车台账列表
     * 
     * @param ledgerForklift 叉车台账
     * @return 叉车台账
     */
    @Override
    public List<LedgerForklift> selectLedgerForkliftList(LedgerForklift ledgerForklift)
    {
        return ledgerForkliftMapper.selectLedgerForkliftList(ledgerForklift);
    }

    /**
     * 新增叉车台账
     * 
     * @param ledgerForklift 叉车台账
     * @return 结果
     */
    @Override
    public int insertLedgerForklift(LedgerForklift ledgerForklift)
    {
        ledgerForklift.setCreateTime(DateUtils.getNowDate());
        int i = ledgerForkliftMapper.insertLedgerForklift(ledgerForklift);
        // 添加图片
        if(ledgerForklift.getImgs() != null && ledgerForklift.getImgs().length > 0){
            for(String path : ledgerForklift.getImgs()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("6");    // 叉车台账
                ledgerFile.setLedgerId(ledgerForklift.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerForklift.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 修改叉车台账
     * 
     * @param ledgerForklift 叉车台账
     * @return 结果
     */
    @Override
    public int updateLedgerForklift(LedgerForklift ledgerForklift)
    {
        ledgerForklift.setUpdateTime(DateUtils.getNowDate());
        int i = ledgerForkliftMapper.updateLedgerForklift(ledgerForklift);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(ledgerForklift.getId());
        ledgerFile.setLedgerType("6");      // 叉车台账
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        if(ledgerForklift.getImgs() != null && ledgerForklift.getImgs().length > 0){
            for(String path : ledgerForklift.getImgs()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("6");    // 叉车台账
                ledgerFile.setLedgerId(ledgerForklift.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerForklift.getUpdateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 批量删除叉车台账
     * 
     * @param ids 需要删除的叉车台账ID
     * @return 结果
     */
    @Override
    public int deleteLedgerForkliftByIds(Long[] ids)
    {
        int i = ledgerForkliftMapper.deleteLedgerForkliftByIds(ids);
        for(Long id : ids){
            // 删除台账图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("6");      // 叉车台账
            ledgerFileMapper.deleteLedgerImg(ledgerFile);
        }
        return i;
    }

    /**
     * 删除叉车台账信息
     * 
     * @param id 叉车台账ID
     * @return 结果
     */
    @Override
    public int deleteLedgerForkliftById(Long id)
    {
        int i = ledgerForkliftMapper.deleteLedgerForkliftById(id);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("6");      // 叉车台账
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        return i;
    }

    @Override
    public String importForklift(List<LedgerForklift> forkliftList, Boolean isUpdateSupport, String operName, Long stationId) {
        if (StringUtils.isNull(forkliftList) || forkliftList.size() == 0)
        {
            throw new CustomException("导入叉车台账不能为空！");
        }
        if (stationId == null){
            throw new CustomException("角色异常！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i=1;
        for (LedgerForklift forklift : forkliftList){
            i++;
            try{
                if (StringUtils.isBlank(forklift.getCompanyName())){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行企业名称不能为空");
                    continue;
                }
                if (StringUtils.isBlank(forklift.getBusinessLicenseNo())){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行营业执照号不能为空");
                    continue;
                }
                if (StringUtils.isBlank(forklift.getLicensePlate())){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行叉车号牌不能为空");
                    continue;
                }
                if (StringUtils.isNull(forklift.getYearlyInspectionDate())){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行年检日期不能为空");
                    continue;
                }
                forklift.setStationId(stationId);
                forklift.setCreateId(String.valueOf(stationId));
                LedgerBusinessAccount account = ledgerBusinessAccountMapper.selectLedgerBusinessAccount(forklift.getCompanyName(), forklift.getBusinessLicenseNo(),stationId);
                if (account == null){
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行、叉车 " + forklift.getLicensePlate() + " 企业信息有误");
                    continue;
                }
                forklift.setBusinessId(account.getId());
                LedgerForklift f = ledgerForkliftMapper.selectLedgerForkliftByLicensePlate(forklift.getLicensePlate(),forklift.getBusinessId());
                if (StringUtils.isNull(f)){
                    forklift.setCreateBy(operName);
                    forklift.setCreateTime(DateUtils.getNowDate());
                    this.insertLedgerForklift(forklift);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、叉车 " + forklift.getLicensePlate() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    forklift.setUpdateBy(operName);
                    forklift.setUpdateId(String.valueOf(stationId));
                    this.updateLedgerForklift(forklift);
                    successNum++;
                    successMsg.append("<br/>第" + i + "行、叉车 " + forklift.getLicensePlate() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>第" + i + "行、叉车 " + forklift.getLicensePlate() + " 已存在");
                }

            }catch (Exception e){
                failureNum++;
                String msg = "<br/>第" + i + "行、叉车 " + forklift.getLicensePlate() + " 导入失败：";
                failureMsg.append(msg + "该行数据信息有误，请仔细检查");
                log.error(msg, e);
            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String checklicensePlateUnique(String licensePlate,Long businessId) {
        LedgerForklift forklift = ledgerForkliftMapper.selectLedgerForkliftByLicensePlate(licensePlate,businessId);
        if (forklift != null){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checklicensePlateUnique2(LedgerForklift ledgerForklift) {
        Long id = StringUtils.isNull(ledgerForklift.getId()) ? -1L : ledgerForklift.getId();
        LedgerForklift forklift = ledgerForkliftMapper.selectLedgerForkliftByLicensePlate(ledgerForklift.getLicensePlate(),ledgerForklift.getBusinessId());
        if (StringUtils.isNotNull(forklift) && forklift.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
